<?php 
/**
* school_admin入口
*/
class School_admin_teacher extends CI_Controller
{
    
    function __construct()
    {
        parent::__construct();
        //登陆状态检测
        if(!$this->session->userdata('r_user_in'))
            redirect('home');
        //权限检测
        if($this->session->userdata('r_role_id') != 4)
            redirect('home');
    }

    /**
     * 学校管理员管理教师的主页面
     * @return [type]
     */
    function index()
    {
        //准备数据
        $data = array();
        $data['title']='教师管理';
        $this->load->model('teacher_model');
        $school_id=$this->session->userdata('r_school_id');
        $data['teachers']=$this->teacher_model->get_all_teachers_by_school_id($school_id);
        
        $this->load->view('school_admin_teacher/index',$data);
    }
    //查看教师的逻辑班
    function check_logic_class($teacher_id)
    {
        //准备数据
        $data = array();
        $data['title']='学生管理';
        $this->load->model('student_model');
        $this->load->model('major_model');
        //$this->load->model('class_model');
        $this->load->model('logic_class_model');

        $this->load->model('teacher_model');
        $teacher=$this->teacher_model->get_by_id($teacher_id);
        
        $logic_classes=$this->logic_class_model->get_logic_classes_by_teacher_id($teacher['id']);
        $data['logic_classes']=$logic_classes;
        $data['teacher_id']=$teacher_id;
        $this->load->view('school_admin_teacher/check_logic_class',$data);
    }
    /**
     * 查看某个逻辑班里的学生
     * @return [type] [description]
     */
    function check_students($logic_class_id,$teacher_id)
    {
        $this->load->model('logic_class_model');
        $logic_class=$this->logic_class_model->get_logic_class_by_id($logic_class_id);
        if(!$logic_class) return $this->load->view('error');
        $this->load->model('student_model');
        $students=$this->student_model->get_students_by_logic_class_id($logic_class_id);
        $this->load->model('teacher_model');
        $me=$this->teacher_model->get_by_username($this->session->userdata('r_username'));

        $data['logic_class']=$logic_class;
        $data['teacher_id']=$teacher_id;
        $data['students']=$students;
        $data['title']='查看学生';
        $this->load->view('school_admin_teacher/check_students',$data);
    }
    //删除一个教师
    function delete($teacher_id)
    {
        $school_id=$this->session->userdata('r_school_id');
        $this->load->model('teacher_model');
        $this->load->model('logic_class_model');
        $this->load->model('user_model');
        $this->load->model('module_model');
        $teacher=$this->teacher_model->get_by_id($teacher_id);
        if(!$teacher)
            return $this->load->view('error');
        if($teacher['school_id']!=$school_id)
            return $this->load->view('error');
        
        $relative_logic_class=$this->logic_class_model->get_logic_classes_by_teacher_id($teacher_id);
        //如果有相关的逻辑班，禁止删除
        if($relative_logic_class)
        {
            $this->session->set_flashdata('message','存在教师的逻辑班，禁止删除');
            redirect('school_admin_teacher/index');
        }
        //如果有相关的模块，禁止删除
        $relative_module=$this->module_model->get_by_teacher_id($teacher_id);
        if($relative_module)
        {
            $this->session->set_flashdata('message','存在教师定制的模块，禁止删除');
            redirect('school_admin_teacher/index');
        }
        //将教师的注册信息从数据库删除
        if($this->teacher_model->delete($teacher_id))
        {
            if($this->user_model->delete($teacher['user_id']))
            {
                $this->session->set_flashdata('message','操作成功');
                redirect('school_admin_teacher/index');
            }
            else
            {
                $this->session->set_flashdata('message','操作失败');
                redirect('school_admin_teacher/index');  
            }
        }
        else
        {
            $this->session->set_flashdata('message','操作失败');
            redirect('school_admin_teacher/index');  
        }
    }
    //重置教师用户密码为用户名
    function reset_teacher_password($teacher_id)
    {
        echo $teacher_id;
        $school_id=$this->session->userdata('r_school_id');
        $this->load->model('teacher_model');
        $this->load->model('user_model');
        $teacher=$this->teacher_model->get_by_id($teacher_id);
        if(!$teacher)
            return $this->load->view('error');
        if($teacher['school_id']!=$school_id)
        {
            $this->session->set_flashdata('message','非本校教师，您没有操作权限');
            redirect('school_admin_teacher/index');
        }
        $teacher_user=$this->user_model->get_by_id($teacher['user_id']);
        if($teacher_user)
        {
            $teacher_user['password']=md5($teacher_user['username']);
            if($this->user_model->update($teacher_user['id'],$teacher_user))
            {
                $this->session->set_flashdata('message','重置密码成功');
                redirect('school_admin_teacher/index');
            }
            else
            {
                $this->session->set_flashdata('message','重置密码失败');
                redirect('school_admin_teacher/index');
            }
                
        }
        else{
            $this->session->set_flashdata('message','用户不存在，重置密码失败');
            redirect('school_admin_teacher/index');
        }
    }
}